

package org.argeproje.resim.actions.project;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectOutputStream;

import org.argeproje.resim.ResimProjectEditor;
import org.argeproje.resim.ShapesEditorInput;
import org.argeproje.resim.WindowUT;
import org.argeproje.resim.ui.model.ShapesDiagram;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.core.runtime.IPath;

import java.io.FileOutputStream;

public class NewProjectACT extends Action {

	private IWorkbenchWindow workbenchWindow;
	private boolean newFile;
	public NewProjectACT(IWorkbenchWindow window, String id, String displayName,
			boolean newFile) {
		setId(id);
		setText(displayName);
		workbenchWindow = window;
		this.newFile = newFile;
	}

	private byte[] getInitialContents() {
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(baos);
			oos.writeObject(createDefaultContent()); // argument must be
														// Serializable
			oos.flush();
			oos.close();
			return baos.toByteArray();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		return null;
	}

	/** Return a new ShapesDiagram instance. */
	private Object createDefaultContent() {
		return new ShapesDiagram();
	}

	/*
	 * protected IFile createFileHandle(IPath filePath) { return
	 * ResourcesPlugin.getWorkspace().getRoot().getProject().getFile(filePath) }
	 */

	private IPath LoadContent(String path) {
		if (!newFile) {
			// TODO Dosya varsa d�ns�n
			return Path.fromOSString(path);
		} else {
			try {
				FileOutputStream fos = new FileOutputStream(path);
				byte[] lBuffer = getInitialContents();
				if (lBuffer != null) {
					fos.write(lBuffer);
				}
				fos.close();// sonradan eklendi...
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return Path.fromOSString(path);
		}
	}

	public void run() {
		String lPath = null;
		if (!newFile) {
			lPath = WindowUT.openFileDialog("rsm", workbenchWindow);
		} else {
			lPath = WindowUT.saveFileDialog("rsm", workbenchWindow, "New .rsm File");
		}
		if (lPath != null) {
			IEditorInput input = null;
			input = new ShapesEditorInput(LoadContent(lPath));
			try {
				if(workbenchWindow.getActivePage().getActiveEditor() != null){
					IAction closeAction = ActionFactory.CLOSE.create(workbenchWindow);
					closeAction.run();	
				}
				
			    workbenchWindow.getActivePage().openEditor(input,
							ResimProjectEditor.EDITOR_ID, true);	
				
		
			} catch (PartInitException e) {
				e.printStackTrace();
			}
		}
	}
}
